﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="sys_dept_edit.aspx.cs" Inherits="Ccit.SysPlat.Organ.sys_dept_edit" %>
<%@ Register TagPrefix="ccit" Namespace="Ccit.ServerControls" Assembly="Ccit.ServerControls.ActionForm" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link rel="Stylesheet" href="../css/main.css" type="text/css" />
    <script src="../script/jquery.js" type="text/javascript"></script>
    <script src="../script/StringClass_Ext.js" type="text/javascript"></script>
    <script src="../script/jwindow.js" type="text/javascript"></script>
    <script src="../script/jcontrol.js" type="text/javascript"></script>
    <script src="../script/jbase64.js" type="text/javascript"></script>
    <script src="../script/jsonlib.js" type="text/javascript"></script>
    <script type="text/javascript">
      <!--
        function doCheck(vType) {
            switch (vType) {
                case "DEPTNAME":
                    var vOrganId = $get("_hidOrganId").value;
                    var ctrlDeptName = $get("txtDeptName");
                    if (jQuery.trim(ctrlDeptName.value) == "") {
                        alert("部门名称不能为空!");
                        ctrlDeptName.focus();
                    }
                    else {
                        var vDeptId = $get("_hidDeptId").value;
                        var vDeptName = jQuery.trim(ctrlDeptName.value);
                        PageMethods.CheckDeptNameExist(vDeptId, vDeptName, vOrganId, ClientCallBack);
                    }
                    break;
                case "ORDER":
                    var ctrlOrder = $get("txtDeptOrder");
                    var vOrder = jQuery.trim(ctrlOrder.value);
                    if (vOrder != "") {
                        if (!vOrder.IsNumber()) {
                            alert("显示顺序必须为数字!");
                            ctrlOrder.focus();
                        }
                    }
                    break;
            }
        }

        function doCheckSaving() {
            var ctrlDeptName = $get("txtDeptName");
            var vDeptName = jQuery.trim(ctrlDeptName.value);
            if (vDeptName == "") {
                alert("部门名称不能为空!");
                ctrlDeptName.focus();
                return false;
            }

            var ctrlOrder = $get("txtDeptOrder");
            var vOrder = jQuery.trim(ctrlOrder.value);
            if (vOrder != "") {
                if (!vOrder.IsNumber()) {
                    alert("显示顺序必须为数字!");
                    ctrlOrder.focus();
                    return false;
                }
            }
            return true;
        }

        function ClientCallBack(vResult) {
            eval("resultObj=" + vResult);
            var vOperType = resultObj.operType;
            switch (vOperType) {
                case "DEPTNAMECHECK":  //部门同名校验                    
                    if (resultObj.resultSign == "1") {
                        alert("已存在同名部门!");
                        var ctrlDeptName = $get("txtDeptName");
                        ctrlDeptName.focus();
                    }
                    break;
                case "DEPTSAVE":  //部门保存   
                    var vCtrlType = resultObj.ctrlType;
                    var vDeptId = resultObj.deptId;
                    var vUpDeptId = resultObj.upDeptId;
                    var isAddNew = ($get("_hidDeptId").value == "") ? "0" : "1";  //是否是新增记录标志  0:是 1:否  
                    alert(resultObj.resultMsg);
                    if (parseInt(vDeptId) > 0) {
                        var curDeptObj = {};
                        curDeptObj.deptId = vDeptId;
                        curDeptObj.deptName = resultObj.deptName;
                        curDeptObj.isHasChild = resultObj.isHasChild;
                        var curDeptExpr = JSON.stringify(curDeptObj, function(key, value) { return value; });
                        switch (vCtrlType) {
                            case "1":
                                $get("_hidDeptId").value = vDeptId;
                                var ctrlDel = $get("btnDel");
                                ctrlDel.style.visibility = "visible";
                                $get("txtDeptOrder").value = resultObj.deptOrder;
                                break;
                            case "2":
                                initializeAddNew();
                                break;
                        }
                        parent.frames[0].document.getElementById("_hidUpDeptId").value = vUpDeptId; //此处与获取_hidUpDeptId值应一样              
                        parent.frames[0].document.getElementById("_hidOriginUpDeptId").value = $get("_hidOriginUpDeptId").value;
                        parent.frames[0].document.getElementById("_hidAddNewSign").value = isAddNew;  //新增记录标志
                        parent.frames[0].document.getElementById("_hidCurDeptExpr").value = curDeptExpr; //当前部门对象表达式
                        parent.frames[0].document.all.btnTrigger.click();
                        $get("_hidOriginUpDeptId").value = vUpDeptId;

                        curDeptObj = null;
                    }                    
                    break;
                case "DEPTDELETE":
                    var vDelSign = resultObj.resultSign;
                    if (parseInt(vDelSign) > 0) {
                        alert("删除成功!");
                        parent.frames[0].document.getElementById("_hidDelDeptId").value = vDelSign;
                        parent.frames[0].document.all.btnDelTrigger.click();
                        initializeAddNew();
                    }
                    else
                        alert("删除失败,请稍后再试!");
                    break;
            }

            resultObj = null;
        }

        function doSave(vCtrlType) {
            if (!doCheckSaving()) return;

            saveDept(vCtrlType);
        }

        function saveDept(ctrlType) {
            var organId = $get("_hidOrganId").value;
            var deptId = $get("_hidDeptId").value;
            var deptName = $get("txtDeptName").value;
            var upDeptId = $get("_hidUpDeptId").value;
            var orderNo = $get("txtDeptOrder").value;
            var deptDesc = $get("txtDeptDesc").value;
            var deptUserIds = $get("_hidUserId").value;
            var adminUserId = $get("_hidManagerId").value;
            
            var msDept = {};
            msDept.deptId = deptId;
            msDept.deptName = deptName;
            msDept.deptDesc = deptDesc;
            msDept.adminUserId = adminUserId;
            msDept.orderNo = orderNo;
            msDept.upDeptId = upDeptId;

            var msOrgan = {};
            var msKeyValue = {};

            msOrgan.organId = organId;
            msKeyValue.key = deptUserIds;
            msDept.belongOrgan = msOrgan;
            msDept.includeUsers = msKeyValue;

            var jsonExpr = JSON.stringify(msDept, function(key, value) { return value; });
            PageMethods.SaveDept(jsonExpr, ctrlType, ClientCallBack);

            msDept = null;         
        }

        function doComboxIndexChange() {
            //$get("_hidUpDeptId").value = cbxUpDept.GetValue();
            var ddlUpDept = new DropDownList("ddlUpDept");
            $get("_hidUpDeptId").value = ddlUpDept.selectedValue;
            ddlUpDept = null;
        }

        function getSelUser() {
            var ctrlLstUser = $get("lstUserName");
            var oOptionArray = ctrlLstUser.options;
            var iLen = ctrlLstUser.options.length;
            var oOption;
            var vSelUserId = "";
            var vSelUserName = "";
            var vRetXML = new StringBuilder;

            for (var i = 0; i < iLen; i++) {
                oOption = oOptionArray[i];
                if (vSelUserId == "") {
                    vSelUserId = oOption.value;
                    vSelUserName = oOption.text;
                }
                else {
                    vSelUserId += "," + oOption.value;
                    vSelUserName += "\\n" + oOption.text;
                }
            }
            vRetXML.append("<USERIDS>").append(vSelUserId).append("</USERIDS>");
            vRetXML.append("<USERNAMES><![CDATA[").append(vSelUserName).append("]]></USERNAMES>");

            return vRetXML.toString();
        }

        function doDelete() {
            if (!confirm("删除当前部门及所有下级子部门,是否继续?"))
                return;

            var vDeptId = $get("_hidDeptId").value;
            PageMethods.DeleteDept(vDeptId, ClientCallBack);
        }

        function initializeAddNew() {
            $get("_hidDeptId").value = "";
            //$get("_hidUpDeptId").value = $get("ddlUpDept").options[$get("ddlUpDept").selectedIndex].value;
            __doPostBack("lnbCallBack", "");
        }

        function clearSelectItems(pCtrl) {
            var iLen = pCtrl.options.length;
            if (iLen == 0) return;

            for (var i = (iLen - 1); i >= 0; i--) {
                pCtrl.options.remove(i);
            }
        }

        function doSelUser(vCtrlId, vCtrlName) {
            OpenUserMultiSelect(vCtrlId, vCtrlName, "0");
        }

        function doSetManager() {
            var olstUserName = $get("lstUserName");
            var iLen = olstUserName.options.length;
            if (iLen == 0) return;

            if (olstUserName.selectedIndex == -1) return;

            var oOption = olstUserName.options[olstUserName.selectedIndex];
            if (oOption == "undefined") return;

            $get("_hidManagerId").value = oOption.value;
            $get("txtManager").value = oOption.innerText;
        }

        function doUnSet() {
            $get("_hidManagerId").value = "";
            $get("txtManager").value = "";
        }

        function doChangeUserName() {
            var vUserId = $get("_hidUserId").value;
            var vUserName = $get("txtUserName").value;
            var vUserIdArray = vUserId.split(",");
            var vUserNameArray = vUserName.split(" ");
            var olstUserName = $get("lstUserName");
            clearSelectItems(olstUserName);

            var oOption;
            for (var i = 0; i < vUserIdArray.length; i++) {
                oOption = document.createElement("<option value='" + vUserIdArray[i] + "'>");
                oOption.innerText = vUserNameArray[i];
                olstUserName.appendChild(oOption);
            }

            // deal dept manager
            var deptManagerId = $get("_hidManagerId").value;
            if (deptManagerId == "") return;

            if (vUserId != "") {
                vUserId = "," + vUserId + ",";
                deptManagerId = "," + deptManagerId + ",";
                var vIndex = vUserId.indexOf(deptManagerId);
                if (vIndex == -1) {
                    $get("_hidManagerId").value = "";
                    $get("txtManager").value = "";
                }
            }
            else {
                $get("_hidManagerId").value = "";
                $get("txtManager").value = "";
            }
        }
          
      //-->
      </script>
</head>
<body scoll="no">
    <ccit:ActionForm id="form1" runat="server">        
    <input type="hidden" id="_hidOrganId" runat="server" />
    <input type="hidden" id="_hidDeptId" runat="server" />
    <input type="hidden" id="_hidOriginUpDeptId" runat="server" />
    <input type="hidden" id="_hidUpDeptId" runat="server" />
    <asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" runat="server">
    </asp:ScriptManager>
        <table class="tableborder" cellpadding="0" cellspacing="0" width="100%" align="center">
            <tr>
                <td width="10%" class="pformleft" align="right"><nobr>机构名称</nobr></td>
                <td width="90%" colspan="5" class="pformright">
                    <font size='2' color='green'><b>
                        &nbsp;<asp:Label ID="lbOrganName" runat="server"></asp:Label>
                    </b></font>
                </td>
            </tr>
            <tr>
                <td width="10%" class="pformleft" align="right"><nobr>上级部门</nobr></td>
                <td width="90%" colspan="3" class="pformright">                    
                    <asp:DropDownList ID="ddlUpDept" onchange="doComboxIndexChange()" CssClass="select" Width="99.3%" runat="server"></asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td width="10%" class="pformleft" align="right"><nobr><font color='red'>*</font>部门名称</nobr></td>
                <td width="40%" class="pformrightline">
                    <asp:TextBox ID="txtDeptName" onchange="doCheck('DEPTNAME')" runat="server" CssClass="input" Width="98%"></asp:TextBox>
                </td>
                <td width="10%" class="pformleft" align="right"><nobr>显示顺序</nobr></td>
                <td width="40%" class="pformright">
                    <asp:TextBox ID="txtDeptOrder" runat="server" onchange="doCheck('ORDER')" CssClass="input" Width="60px" style="direction:rtl"></asp:TextBox>
                    &nbsp;<font color="blue" face='Arial' size='1.5'><i><b>注: 为空则由系统默认生成</b></i></font>
                </td>
            </tr>
            <tr>
                <td width="10%" class="pformleft" align="right" valign="top"><nobr>部门描述</nobr></td>
                <td width="90%" class="pformright" colspan="3">
                    <asp:TextBox id="txtDeptDesc" TextMode="MultiLine" Width="99.3%" rows="4" runat="server"></asp:TextBox>
                </td>
            </tr>
            <%--<tr>
                <td width="10%" class="pformleft" align="right"><nobr>部门成员</nobr></td>
                <td width="90%" class="pformright" colspan="3" align="right">
                    <nobr>
                        <a href="javascript:doSelUser('_hidUserId','txtUserName')">
                            <img src="../images/toolbar/user.gif" border="0" />成员选择
                        </a>&nbsp;
                        <a href="javascript:doSetManager()">
                            <img src="../images/toolbar/grtxl.gif" border="0" />设为主管
                        </a>                        
                    </nobr>
                </td>
            </tr>--%>
            <tr>
                <td width="10%" class="pformleft" align="right" valign="top"><nobr>部门成员</nobr></td>
                <td width="90%" class="pformright" colspan="3">
                    <table cellpadding="0" cellspacing="0" width="100%">
                        <tr>
                            <td width="90%">
                                <input type="hidden" id="_hidUserId" runat="server" />
                                <asp:TextBox ID="txtUserName" style="display:none" onpropertychange="doChangeUserName()"  runat="server"></asp:TextBox>
                                <asp:ListBox ID="lstUserName" CssClass="select" Width="99.3%" rows="6" runat="server"></asp:ListBox>  
                            </td>
                            <td align="right" valign="top">
                                <table cellpadding="0" cellspacing="0" width="100%">
                                    <tr>
                                        <td align="center">
                                             <nobr>
                                                <a href="javascript:doSelUser('_hidUserId','txtUserName')">
                                                    <img src="../images/toolbar/selUser.gif" border="0" />成员选择
                                                </a>
                                              </nobr>
                                        </td>
                                     </tr>
                                     <tr>
                                        <td align="center">
                                              <nobr>
                                                 <a href="javascript:doSetManager()">
                                                        <img src="../images/toolbar/setAdminUser.gif" border="0" />设为主管
                                                    </a>                        
                                                </nobr>
                                        </td>
                                    </tr>
                                </table>                               
                            </td>
                        </tr>                        
                    </table>                                      
                </td>
            </tr>
            <tr>
                <td width="10%" class="pformleft" align="right"><nobr>部门主管</nobr></td>
                <td width="90%" class="pformright" colspan="3">
                    <nobr>
                        <input type="hidden" id="_hidManagerId" runat="server" />
                        <asp:TextBox ID="txtManager" runat="server" CssClass="inputreadonly" Width="100px"></asp:TextBox> 
                        <a href="javascript:doUnSet()">
                            <img src="../images/toolbar/cancel.gif" border="0" />取消
                        </a>
                    </nobr>
                </td>
            </tr>
            <tr class="pformstrip">
                <td colspan="6" align="center">
                    <input type="button" id="btnSave" onclick="doSave('1')" value="保存" class="button" />
                    &nbsp;
                    <input type="button" id="btnSaveAdd" onclick="doSave('2')" value="保存并新增" class="button" />
                    &nbsp;
                    <input type="button" id="btnDel" onclick="doDelete()" value="删除" class="button" />
                </td>
            </tr>
        </table>
        <asp:LinkButton ID="lnbCallBack" style="display:none"  runat="server" onclick="lnbCallBack_Click"></asp:LinkButton>
   
    <script language="javascript" type="text/javascript">
    <!--        
        $(document).ready(function(){
            var deptId = $get("_hidDeptId").value;
            var ctrlDel = $get("btnDel");
            if (deptId == "")
                ctrlDel.style.visibility = "hidden";
            else
                ctrlDel.style.visibility = "visible";
        });
    //-->
    </script>
    </ccit:ActionForm>
</body>
</html>
